/**
 * @author David Young
 */
package csab.tests.visual;

import javax.swing.JOptionPane;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
//import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
import org.newdawn.slick.util.Log;

import csab.core.Entity;
import csab.core.ResourceManager;
import csab.core.Sprite;
import csab.core.View;
import csab.game.sprites.PlayerSprite;

/**
 * Visual test for the View class.
 * @author David Young
 * @see csab.core.View
 */
public class VisualViewTest extends BasicGame {
	private TiledMap map;
	private Sprite ts;
	private View viewport;
	private ResourceManager mgr;
	private Font testfont;
	
	/**
	 * Entry point for the test.
	 * @param args Not parsed.
	 * @throws SlickException
	 */
	public static void main(String [] args) throws SlickException { 
		AppGameContainer gc = new AppGameContainer(new VisualViewTest());
		float aspect = (float)gc.getScreenWidth() / gc.getScreenHeight();
		//Calculate for aspect ratios
		float epsilon = 0.01f;
		if(Math.abs(aspect - (float)16/9) < epsilon)
			gc.setDisplayMode(1280, 720, false);
		else if(Math.abs(aspect - (float)4/3) < epsilon)
			gc.setDisplayMode(800, 600, false);
		else if(Math.abs(aspect - (float)16/10) < epsilon)
			gc.setDisplayMode(1280, 800, false);
		else {
			JOptionPane.showMessageDialog(null, 
					"Your computer does not support any resolutions "+
					"compatible with this game. Sorry.",
					"Unsupported Resolution", 
					JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}
		gc.setClearEachFrame(false);
		gc.setUpdateOnlyWhenVisible(true);
		gc.setFullscreen(false);
		gc.start();
	}

	public VisualViewTest() {
		super("Visual View Test");
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
	 */
	@Override
	public void init(GameContainer gc) throws SlickException {
		mgr = ResourceManager.getManager("resource/resources.xml");
		testfont = mgr.getFont("speech_small");
		String mapname = "resource/maps/TestMap2.tmx";
		try{
			map = new TiledMap(mapname);
		}
		catch(Exception e)
		{
			Log.error("Could not load map :: " + mapname);
			JOptionPane.showMessageDialog(null, "Could not load map ::\n" + mapname,
												"Load Error", 
												JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}
		
		viewport = new View(0, 0, gc.getWidth(), gc.getHeight(), map.getWidth() * map.getTileWidth(),
											map.getHeight() * map.getTileHeight());
		ts = new PlayerSprite("Player");
		ts.setPos(400, 300);
		Entity.setWorldXRange(0, map.getWidth() * map.getTileWidth());
		Entity.setWorldYRange(0, map.getHeight() * map.getTileHeight());
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.Game#render(org.newdawn.slick.GameContainer, org.newdawn.slick.Graphics)
	 */
	@Override
	public void render(GameContainer arg0, Graphics arg1) throws SlickException {
		String msg = "Woot this is what a paragraph looks like in\n" +
					 "Angelcode, isn't it awesome? YEAH. MURICA.";
		//bg.draw(0,0, arg0.getWidth(), arg0.getHeight());
		for(int i = 0; i < map.getLayerCount()-1; i++)
			viewport.renderTileMap(map, i);
		//viewport.renderTileMap(map);
		viewport.drawEntity(arg1, ts);
		viewport.renderTileMap(map, map.getLayerCount()-1);
		arg1.setFont(testfont);
		arg1.setColor(Color.black);
		arg1.drawString(msg, 
						arg0.getWidth()/2 - (testfont.getWidth(msg)/2), 
						arg0.getHeight()/2 - (testfont.getHeight(msg)));
	}
	
	/* (non-Javadoc)
	 * @see org.newdawn.slick.BasicGame#update(org.newdawn.slick.GameContainer, int)
	 */
	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		Input keyboard = gc.getInput();
		if(keyboard.isKeyDown(Input.KEY_ESCAPE))
			gc.exit();
		
		if(keyboard.isKeyDown(Input.KEY_D))
		{
			ts.setVX(100);
		}
		else if(keyboard.isKeyDown(Input.KEY_A))
		{
			ts.setVX(-100);
		}
		else
			ts.setVX(0);
		
		if(keyboard.isKeyDown(Input.KEY_S))
		{
			ts.setVY(100);
		}
		else if(keyboard.isKeyDown(Input.KEY_W))
		{
			ts.setVY(-100);
		}
		
		ts.doMove(delta, 0, 50);
		viewport.center(ts);
	}

}
